home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 351-375 / 351 / pdc / pdcsrc.lzh / PDC / Force.c < prev    next >
C/C++ Source or Header  |  1990-04-06  |  14KB  |  491 lines

  1.  
  2. /* PDC Compiler - A Freely Distributable C Compiler for the Amiga
  3.  *                Based upon prior work by Matthew Brandt and Jeff Lydiatt.
  4.  *
  5.  * PDC Compiler release 3.3 Copyright (C) 1989 Paul Petersen and Lionel Hummel.
  6.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  7.  *
  8.  * This code is freely redistributable upon the conditions that this 
  9.  * notice remains intact and that modified versions of this file not be 
  10.  * distributed as part of the PDC Software Distribution without the express
  11.  * consent of the copyright holders.
  12.  *
  13.  *------------------------------------------------------------------
  14.  *
  15.  * $Log:    Force.c,v $
  16.  * Revision 3.33  90/04/05  23:04:41  lionel
  17.  * None.
  18.  * 
  19.  * Revision 3.32  90/02/03  16:23:44  lionel
  20.  * None
  21.  * 
  22.  *------------------------------------------------------------------
  23.  */
  24.  
  25. /* Force.c - Performs type conversions for binary and assignment ops
  26.  */
  27.  
  28. #include    <stdio.h>
  29. #include    "C.h"
  30. #include    "Expr.h"
  31. #include    "Gen.h"
  32. #include    "Cglbdec.h"
  33.  
  34. extern TYP stdint;
  35. extern TYP stdunsigned;
  36. extern TYP stdchar;
  37. extern TYP stdshort;
  38. extern TYP stdstring;
  39. extern TYP stdfunc;
  40. extern TYP stdfloat;
  41. extern TYP stddouble;
  42.  
  43. extern struct enode *makenode();
  44. extern int          isscalar();
  45. extern void         error();
  46.  
  47. void
  48. conv_signed( node, cmd )
  49.     struct enode    **node;
  50.     enum e_node     cmd;
  51. {
  52.     if (node != NULL) {
  53.         *node = makenode( cmd, *node, NULL );
  54.         (*node)->signedflag = 1;
  55.     }
  56. }
  57.  
  58. void
  59. conv_unsigned( node, cmd )
  60.     struct enode    **node;
  61.     enum e_node     cmd;
  62. {
  63.     if (node != NULL) {
  64.         *node = makenode( cmd, *node, NULL );
  65.         (*node)->signedflag = 0;
  66.     }
  67. }
  68.  
  69. /*
  70.  * asforcefit will coerce the nodes passed into compatable types and return
  71.  * the type of the resulting expression.
  72.  * 
  73.  * NOTE: This routine is only called during assignment statements. It is
  74.  * supposed to make ival = dval a type mismatch instead of promoting ival to
  75.  * a double. e.g. ival*dval and dval*ival
  76.  * 
  77.  */
  78.  
  79. TYP            *
  80. asforcefit(node1, tp1, node2, tp2)
  81.     struct enode  **node1, **node2;
  82.     TYP            *tp1, *tp2;
  83. {
  84.     node1 = node1;
  85.  
  86.     switch (tp1->type) {
  87.     case bt_char:
  88.         switch (tp2->type) {
  89.         case bt_char:
  90.         case bt_short:
  91.         case bt_enum:
  92.         case bt_long:
  93.             return( tp1 );
  94.         case bt_uchar:
  95.         case bt_ushort:
  96.         case bt_unsigned:
  97.             return( tp1 );
  98.         case bt_float:
  99.             conv_signed( node2, en_cfl );
  100.             return( tp1 );
  101.         case bt_double:
  102.             conv_signed( node2, en_cdl );
  103.             return( tp1 );
  104.         }
  105.         break;
  106.     case bt_uchar:
  107.         switch (tp2->type) {
  108.         case bt_char:
  109.         case bt_short:
  110.         case bt_enum:
  111.         case bt_long:
  112.             return( tp1 );
  113.         case bt_uchar:
  114.         case bt_ushort:
  115.         case bt_unsigned:
  116.             return( tp1 );
  117.         case bt_float:
  118.             conv_unsigned( node2, en_cfl );
  119.             return( tp1 );
  120.         case bt_double:
  121.             conv_unsigned( node2, en_cdl );
  122.             return( tp1 );
  123.         }
  124.         break;
  125.     case bt_ushort:
  126.         switch (tp2->type) {
  127.         case bt_char:
  128.             conv_signed( node2, en_cbl );
  129.             return( tp1 );
  130.         case bt_uchar:
  131.             conv_unsigned( node2, en_cbl );
  132.             return( tp1 );
  133.         case bt_ushort:
  134.         case bt_short:
  135.         case bt_enum:
  136.         case bt_long:
  137.         case bt_unsigned:
  138.             return( tp1 );
  139.         case bt_float:
  140.             conv_unsigned( node2, en_cfl );
  141.             return( tp1 );
  142.         case bt_double:
  143.             conv_unsigned( node2, en_cdl );
  144.             return( tp1 );
  145.         }
  146.         break;
  147.     case bt_short:
  148.     case bt_enum:
  149.         switch (tp2->type) {
  150.         case bt_char:
  151.             conv_signed( node2, en_cbl );
  152.             return( tp1 );
  153.         case bt_uchar:
  154.             conv_unsigned( node2, en_cbl );
  155.             return( tp1 );
  156.         case bt_ushort:
  157.         case bt_short:
  158.         case bt_enum:
  159.         case bt_long:
  160.         case bt_unsigned:
  161.             return( tp1 );
  162.         case bt_float:
  163.             conv_signed( node2, en_cfl );
  164.             return( tp1 );
  165.         case bt_double:
  166.             conv_signed( node2, en_cdl );
  167.             return( tp1 );
  168.         }
  169.         break;
  170.     case bt_long:
  171.         switch (tp2->type) {
  172.         case bt_pointer:
  173.         case bt_unsigned:
  174.         case bt_long:
  175.             return( tp1 );
  176.         case bt_char:
  177.             conv_signed( node2, en_cbl );
  178.             return( tp1 );
  179.         case bt_uchar:
  180.             conv_unsigned( node2, en_cbl );
  181.             return( tp1 );
  182.         case bt_short:
  183.         case bt_enum:
  184.             conv_signed( node2, en_cwl );
  185.             return( tp1 );
  186.         case bt_ushort:
  187.             conv_unsigned( node2, en_cwl );
  188.             return( tp1 );
  189.         case bt_float:
  190.             conv_signed( node2, en_cfl );
  191.             return( tp1 );
  192.         case bt_double:
  193.             conv_signed( node2, en_cdl );
  194.             return( tp1 );
  195.         }
  196.         break;
  197.     case bt_pointer:
  198.         if (isscalar(tp2) || tp2->type == bt_pointer)
  199.             return tp1;
  200.         if (tp2->type == bt_ifunc || tp2->type == bt_func)
  201.             return tp1;
  202.         break;
  203.     case bt_unsigned:
  204.         if (isscalar(tp2) || tp2->type == bt_pointer)
  205.             return tp1;
  206.         break;
  207.     case bt_union:
  208.     case bt_struct:
  209.         if (tp1->type == tp2->type)
  210.             return (tp1);
  211.         break;
  212.     case bt_float:
  213.         if (tp1->type == tp2->type)
  214.             return (tp1);
  215.         if (tp2->type != bt_double)
  216.             conv_signed( node2, en_clf );
  217.         else
  218.             conv_signed( node2, en_cdf );
  219.         return (tp1);
  220.         break;
  221.     case bt_double:
  222.         switch (tp2->type) {
  223.         case bt_double:
  224.             return (tp1);
  225.         case bt_float:
  226.             conv_signed( node2, en_cfd );
  227.             return (tp1);
  228.         case bt_long:
  229.             conv_signed( node2, en_cld );
  230.             return (tp1);
  231.         }
  232.         break;
  233.     }
  234.     error(ERR_MISMATCH, NULL);
  235.     return tp1;
  236. }
  237.  
  238. /*
  239.  * forcefit will coerce the nodes passed into compatable types and return the
  240.  * type of the resulting expression.
  241.  */
  242.  
  243. TYP            *
  244. forcefit(node1, tp1, node2, tp2)
  245.     struct enode  **node1, **node2;
  246.     TYP            *tp1, *tp2;
  247. {
  248.     switch (tp1->type) {
  249.     case bt_char:
  250.         switch (tp2->type) {
  251.         case bt_char:
  252.             conv_signed( node1, en_cbl );
  253.             conv_signed( node2, en_cbl );
  254.             return (&stdint);
  255.         case bt_uchar:
  256.             conv_unsigned( node1, en_cbl );
  257.             conv_unsigned( node2, en_cbl );
  258.             return (&stdunsigned);
  259.         case bt_ushort:
  260.             conv_unsigned( node1, en_cbl );
  261.             conv_unsigned( node2, en_cwl );
  262.             return (&stdunsigned);
  263.         case bt_short:
  264.         case bt_enum:
  265.             conv_signed( node1, en_cbl );
  266.             conv_signed( node2, en_cwl );
  267.             return (&stdint);
  268.         case bt_long:
  269.             conv_signed( node1, en_cbl );
  270.             return (&stdint);
  271.         case bt_pointer:
  272.             conv_unsigned( node1, en_cbl );
  273.             return (tp2);
  274.         case bt_unsigned:
  275.             conv_unsigned( node1, en_cbl );
  276.             return (&stdunsigned);
  277.         case bt_float:
  278.             conv_signed( node2, en_cfd );
  279.             /* FALL THRU */
  280.         case bt_double:
  281.             conv_signed( node1, en_cbl );
  282.             conv_signed( node1, en_cld );
  283.             return (&stddouble);
  284.         }
  285.         break;
  286.     case bt_uchar:
  287.         switch (tp2->type) {
  288.         case bt_char:
  289.         case bt_uchar:
  290.             conv_unsigned( node1, en_cbl );
  291.             conv_unsigned( node2, en_cbl );
  292.             return (&stdunsigned);
  293.         case bt_short:
  294.         case bt_ushort:
  295.         case bt_enum:
  296.             conv_unsigned( node1, en_cbl );
  297.             conv_unsigned( node2, en_cwl );
  298.             return (&stdunsigned);
  299.         case bt_pointer:
  300.             conv_unsigned( node1, en_cbl );
  301.             return (tp2);
  302.         case bt_unsigned:
  303.         case bt_long:
  304.             conv_unsigned( node1, en_cbl );
  305.             if (node2 != NULL) 
  306.                 (*node2)->signedflag = 0;
  307.             return (&stdunsigned);
  308.         case bt_float:
  309.             conv_signed( node2, en_cfd );
  310.         case bt_double:
  311.             conv_unsigned( node1, en_cbl );
  312.             conv_signed( node1, en_cld );
  313.             return (&stddouble);
  314.         }
  315.         break;
  316.     case bt_ushort:
  317.         switch (tp2->type) {
  318.         case bt_char:
  319.         case bt_uchar:
  320.             conv_unsigned( node1, en_cwl );
  321.             conv_unsigned( node2, en_cbl );
  322.             return (&stdunsigned);
  323.         case bt_short:
  324.         case bt_ushort:
  325.         case bt_enum:
  326.             conv_unsigned( node1, en_cwl );
  327.             conv_unsigned( node2, en_cwl );
  328.             return (&stdunsigned);
  329.         case bt_pointer:
  330.             conv_unsigned( node1, en_cwl );
  331.             return (tp2);
  332.         case bt_unsigned:
  333.         case bt_long:
  334.             conv_unsigned( node1, en_cwl );
  335.             if (node2 != NULL)
  336.                 (*node2)->signedflag = 0;
  337.             return (&stdunsigned);
  338.         case bt_float:
  339.             conv_signed( node2, en_cfd );
  340.             /* FALL THRU */
  341.         case bt_double:
  342.             conv_unsigned( node1, en_cwl );
  343.             conv_signed( node1, en_cld );
  344.             return (&stddouble);
  345.         }
  346.         break;
  347.     case bt_short:
  348.     case bt_enum:
  349.         switch (tp2->type) {
  350.         case bt_uchar:
  351.             conv_unsigned( node1, en_cwl );
  352.             conv_unsigned( node2, en_cbl );
  353.             return (&stdunsigned);
  354.         case bt_char:
  355.             conv_signed( node1, en_cwl );
  356.             conv_signed( node2, en_cbl );
  357.             return (&stdint);
  358.         case bt_ushort:
  359.             conv_unsigned( node1, en_cwl );
  360.             conv_unsigned( node2, en_cwl );
  361.             return (&stdunsigned);
  362.         case bt_short:
  363.         case bt_enum:
  364.             conv_signed( node1, en_cwl );
  365.             conv_signed( node2, en_cwl );
  366.             return (&stdint);
  367.         case bt_pointer:
  368.             conv_unsigned( node1, en_cwl );
  369.             return (tp2);
  370.         case bt_unsigned:
  371.             conv_unsigned( node1, en_cwl );
  372.             if (node2 != NULL)
  373.                 (*node2)->signedflag = 0;
  374.             return (&stdunsigned);
  375.         case bt_long:
  376.             conv_signed( node1, en_cwl );
  377.             return (&stdint);
  378.         case bt_float:
  379.             conv_signed( node2, en_cfd );
  380.             /* FALL THRU */
  381.         case bt_double:
  382.             conv_signed( node1, en_cwl );
  383.             conv_signed( node1, en_cld );
  384.             return (&stddouble);
  385.         }
  386.         break;
  387.     case bt_long:
  388.         switch (tp2->type) {
  389.         case bt_long:
  390.             return( tp1 );
  391.         case bt_char:
  392.             conv_signed( node2, en_cbl );
  393.             return (&stdint);
  394.         case bt_uchar:
  395.             conv_unsigned( node2, en_cbl );
  396.             if (node1 != NULL)
  397.                 (*node1)->signedflag = 0;
  398.             return (&stdunsigned);
  399.         case bt_ushort:
  400.             conv_unsigned( node2, en_cwl );
  401.             if (node1 != NULL)
  402.                 (*node1)->signedflag = 0;
  403.             return (&stdunsigned);
  404.         case bt_short:
  405.         case bt_enum:
  406.             conv_signed( node2, en_cwl );
  407.             return (&stdint);
  408.         case bt_unsigned:
  409.             if (node1 != NULL)
  410.                 (*node1)->signedflag = 0;
  411.             return (&stdunsigned);
  412.         case bt_pointer:
  413.             return tp2;
  414.         case bt_float:
  415.             conv_signed( node2, en_cfd );
  416.             /* FALL THRU */
  417.         case bt_double:
  418.             conv_signed( node1, en_cld );
  419.             return (&stddouble);
  420.             break;
  421.         }
  422.         break;
  423.     case bt_pointer:
  424.         if (isscalar(tp2) || tp2->type == bt_pointer)
  425.             return tp1;
  426.         if (tp2->type == bt_ifunc || tp2->type == bt_func)
  427.             return tp1;
  428.         break;
  429.     case bt_unsigned:
  430.         switch (tp2->type) {
  431.         case bt_pointer:
  432.             return tp2;
  433.         case bt_uchar:
  434.         case bt_char:
  435.             conv_unsigned( node2, en_cbl );
  436.             return (&stdunsigned);
  437.         case bt_ushort:
  438.         case bt_short:
  439.         case bt_enum:
  440.             conv_unsigned( node2, en_cwl );
  441.             return (&stdunsigned);
  442.         case bt_unsigned:
  443.             return( tp1 );
  444.         case bt_long:
  445.             if (node2 != NULL)
  446.                 (*node2)->signedflag = 0;
  447.             return (&stdunsigned);
  448.         case bt_float:
  449.             conv_signed( node2, en_cfd );
  450.             /* FALL THRU */
  451.         case bt_double:
  452.             conv_signed( node1, en_cld );
  453.             return (&stddouble);
  454.         }
  455.         break;
  456.     case bt_union:
  457.     case bt_struct:
  458.         if (tp1->type == tp2->type)
  459.             return (tp1);
  460.         break;
  461.     case bt_float:
  462.         switch (tp2->type) {
  463.         case bt_float:
  464.             return (tp1);
  465.         case bt_double:
  466.             conv_signed( node1, en_cfd );
  467.             return (&stddouble);
  468.         case bt_long:
  469.             conv_signed( node2, en_clf );
  470.             return (tp1);
  471.         }
  472.         break;
  473.     case bt_double:
  474.         switch (tp2->type) {
  475.         case bt_double:
  476.             return (tp1);
  477.         case bt_float:
  478.             conv_signed( node2, en_cfd );
  479.             return (tp1);
  480.         case bt_long:
  481.             conv_signed( node2, en_cld );
  482.             return (tp1);
  483.         }
  484.         break;
  485.     }
  486.     error(ERR_MISMATCH, NULL);
  487.     return tp1;
  488. }
  489.  
  490.  
  491.